package client.GUI.order;


import javax.swing.*;

import client.GUI.GUIConstants;
import client.GUI.GUIClient;
import client.GUI.dashboard.OpenOrderPanel;
import client.GUI.dashboard.PriceHistoryPanel;
import client.backend.Connection;
import client.backend.price.Price;
import client.backend.price.PriceController;
import client.backend.user.UserInformation;

import java.awt.Color;
import java.awt.event.*;

/**
 * Brings up the new order screen, allows the user to create a new order
 * and gives the option of creating a new Trailing Stop or Limit Order.
 */
public class NewOrderPanel extends JPanel implements ActionListener{

	private static final long serialVersionUID = 1L;
	
		private JButton seePriceHistory, create, createTS, createLO, back;
		private JComboBox currPairs, buySell;
		private JTextField ratio, enterAmount;
		
	/**
	 * This is the default constructor
	 */
	public NewOrderPanel() {
		super();
		initialize();
		
		updateRatio();
	}
	
	/**
	 * Constructor, takes the currency pairs the user wants to deal with
	 * 
	 * @param currPair	- 	The String that holds the currency pair the user wants to trade 
	 */
	
	public NewOrderPanel(String currPair){
		this();
		
		currPairs.setSelectedItem(currPair);
		String [] currs = Price.seperateCurrencies(currPair);
		ratio.setText(PriceController.getInstance().getPrice(currs[0], currs[1]).getBuy() + "");
	}

	/**
	 * This method initialises this
	 */
	private void initialize() {
		//Create the labels
		JLabel title = new JLabel("Create New Limit Order");
		JLabel selectCurr = new JLabel("Select the currencies you wish to trade:");
		JLabel currRatio = new JLabel("Current ratio:");
		JLabel amount = new JLabel("Enter amount of money you wish to trade: ");
		
		//Create the dropdown menus
		currPairs = new JComboBox(PriceController.getInstance().getCurrPairs());
		buySell = new JComboBox(GUIConstants.buySell);
		
		//Create the text fields
		ratio = new JTextField();
		ratio.setEditable(false);
		enterAmount = new JTextField();
		
		//Create the buttons
		seePriceHistory = new JButton("Price History");
		createTS = new JButton("Create a Trailing Stop Order");
		createLO = new JButton("Create a Limit Order");
		create = new JButton("Create");
		back = new JButton("Back");
		
		//Set the bounds to all the components for an apporpriate layout
		title.setBounds(255, 50, 142, 40);
		selectCurr.setBounds(90, 190, 230, 30);
		currPairs.setBounds(370, 190, 96, 30);
		buySell.setBounds(500, 190, 50, 30);
		seePriceHistory.setBounds(600, 190, 150, 30);
		currRatio.setBounds(90, 250, 77, 30);
		ratio.setBounds(184, 250, 100, 30);
		amount.setBounds(90, 350, 240, 30);
		enterAmount.setBounds(370, 350, 50, 30);
		createTS.setBounds(90, 450, 210, 30);
		createLO.setBounds(350, 450, 150, 30);
		create.setBounds(90, 550, 120, 30);
		back.setBounds(370, 550, 120, 30);
		
		//Create the action listeners for all the components
		create.addActionListener(this);
		createTS.addActionListener(this);
		createLO.addActionListener(this);
		currPairs.addActionListener(this);
		buySell.addActionListener(this);
		seePriceHistory.addActionListener(this);
		back.addActionListener(this);
		
		//Set the panel colors
		this.setBackground(Color.black);
		title.setForeground(Color.white);
		selectCurr.setForeground(Color.white);
		currRatio.setForeground(Color.white);
		amount.setForeground(Color.white);
		
		/*I reduced the size of the panel by 50 in order to make room for the bottom panel*/
		this.setSize(1024, 718);
		this.setLayout(null);
		
		//Add all the components
		this.add(title);
		this.add(selectCurr);
		this.add(currPairs);
		this.add(buySell);
		this.add(seePriceHistory);
		this.add(ratio);
		this.add(currRatio);
		this.add(amount);
		this.add(enterAmount);
		this.add(createTS);
		this.add(createLO);
		this.add(create);
		this.add(back);
	}
	
	/**
	 * Check for an actiona and take appropriate steps
	 */
	public void actionPerformed(ActionEvent e) {
		//Create a new order
		if (e.getSource() == create) {
			createNO();
		}
		//Show the price history for the currency pair
		else if (e.getSource() == seePriceHistory) {
			int bS = 2;
			if(buySell.getSelectedItem() == "Buy"){
				bS = 1;
			}
			try {
				PriceHistoryPanel prices = new PriceHistoryPanel(currPairs.getSelectedItem().toString(), bS);
				prices.pack();
				prices.setVisible(true);
			}
			catch (Exception ex) {ex.printStackTrace();}
		}
		//Go back
		else if (e.getSource() == back) {
			GUIClient.getInstance().backFromNewOrder();
		}
		//Update the ratio to match the users buy/sell selection
		else if ((e.getSource() == buySell) || (e.getSource() == currPairs)) {
			updateRatio();
		}
		//Create trailing stop order
		else if (e.getSource() == createTS) {
			GUIClient.getInstance().newTS(currPairs.getSelectedItem(), buySell.getSelectedItem(), enterAmount.getText());
		}
		//Create a limit order
		else if (e.getSource() == createLO) {
			GUIClient.getInstance().newLO(currPairs.getSelectedItem(), buySell.getSelectedItem(), enterAmount.getText());
		}
	}
	
	/**
	 * Display the updated ratio to match the user's buy/sell selection
	 */
	public void updateRatio() {
		String curr1 = currPairs.getSelectedItem().toString().substring(0,3);
		String curr2 = currPairs.getSelectedItem().toString().substring(3,6);
		
		if (buySell.getSelectedItem().toString().equals("Buy")) 
			ratio.setText(PriceController.getInstance().getPrice(curr1, curr2).getBuy()+"");
		else
			ratio.setText(PriceController.getInstance().getPrice(curr1, curr2).getSell()+"");
	}
	
	/**
	 * Create the new order
	 */
	public void createNO() {
		String currPair = currPairs.getSelectedItem().toString();
		String buySell = this.buySell.getSelectedItem().toString();
		String amount = enterAmount.getText();
		String currRatio = ratio.getText();
		
		if (UserInformation.getInstance().getUser().createLO(currPair, buySell, amount, currRatio))
			GUIClient.getInstance().popupNO(currPair, buySell, amount);
		else
			GUIClient.getInstance().popupFail();
		
		GUIClient.getInstance().loggedIn();
	}
}  //  @jve:decl-index=0:visual-constraint="-12,371"


